<?php

namespace app\admin\controller;

session_start();

use think\Controller;
use think\Db;

class Login extends Controller
{

    public function index(){
        return $this->fetch();
    }   //END

    public function save(){
                                                        //获取数据库信息
        $model = db('l_adminuser');
                                                        //验证用户名与密码是否正确
        $list = $model->where([
            'username'=>$_POST['username'], 
            'password'=>md5($_POST['password'])
            ])->select();

        if (empty($list)) {
            $this->error('用户或密码错误!!');            //验证失败返回报错信息
            exit;

        } else {
                                                        //验证成功将数据存入session并返回后台管理页面
            $_SESSION['admin']['id'] = $list[0]['id'];
            $_SESSION['admin']['name'] = $list[0]['name'];
            $_SESSION['admin']['status'] = $list[0]['status'];
            Db::table('l_adminuser')->where('id', $_SESSION['admin']['id'])->update(['time'=>time()]);

            $time = db('l_adminuser')->where('id', $_SESSION['admin']['id'])->select();
            $_SESSION['admin']['time'] = $time[0]['time'];

            $this->redirect('/admin/Index') ;

        }

    }   //END

    public function delete($id){
        if ($_SESSION['admin']['id'] == $id) {
            unset($_SESSION['admin']);
            unset($_SESSION['name']);
            unset($_SESSION['time']);
            unset($_SESSION['status']);
            $this->redirect('/admin/Login');
        }
    }   //END
}